package com.system.stockservice.service.impl;

import com.system.commonutils.bean.ApiResult;
import com.system.commonutils.bean.PagesBean;
import com.system.commonutils.pojo.GoodsType;
import com.system.commonutils.pojo.Stock;
import com.system.commonutils.pojo.Stock_info;
import com.system.stockservice.beans.OrderGoodsNumebrBeans;
import com.system.stockservice.bus.TestSender;
import com.system.stockservice.mapper.StockMapper;
import com.system.stockservice.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: GPC
 * Date: 2020/07/07 8:51
 * Description:
 * Version: V1.0
 */

@Service
@Transactional(rollbackFor = Exception.class)
public class StockServiceImpl implements StockService {

    @Autowired()
    StockMapper mapper;
    @Autowired
    TestSender testSender;

//    @Autowired(required = false)
//    PagesBean pagesBean;

    @Override
    public ApiResult selectStock(Stock stock) {
        System.out.println("service==================================");
        PagesBean pagesBean = new PagesBean();
        pagesBean.setNowsPage(stock.getNowsPage());
        pagesBean.setPageSize(stock.getPageSize());
        int i = (stock.getNowsPage() - 1) * stock.getPageSize();
        stock.setNowsPage(i);
        List<Stock> list = mapper.selectStock(stock);
        System.out.println("list==============" + list.toString());
        Integer totalstocks = mapper.selectTotal(stock);
        System.out.println("总数==============" + totalstocks);
        if (list.size() > 0) {
            pagesBean.setData(list);
            pagesBean.setDataCount(totalstocks);
            System.out.println(pagesBean);
            return ApiResult.successWithData(pagesBean);
        } else {
            return ApiResult.failure("查询失败");
        }
    }


    @Override
    public ApiResult addStock(Stock stock)   {
        System.out.println("方法：serviceimpl==============" + stock);
        Stock result = mapper.selectStockByName(stock);  //通过货物名查询库存中是否有相应货物
        if (result != null && !"".equals(result)) {
            return ApiResult.failure("当前货物已存在");
        }
        Integer i = mapper.addStock(stock);//给库存中添加新的商品
        Stock result1 = mapper.selectStockByName(stock);
        stock.setStock_id(result1.getStock_id());
        stock.setTime(new Date());
        Integer j = mapper.addStockInfo(stock);//添加货物批次信息
        if (i > 0) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("添加失败");
        }
    }



    @Override
    public ApiResult updateStock(Stock stock) {
        Integer i = mapper.updateStock(stock);
        if (i > 0) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("查询失败");
        }
    }


    @Override
    public ApiResult selectinfo(Stock_info stock) {
        PagesBean pagesBean = new PagesBean();
        pagesBean.setNowsPage(stock.getNowsPage());
        pagesBean.setPageSize(stock.getPageSize());
        int i = (stock.getNowsPage() - 1) * stock.getPageSize();
        stock.setNowsPage(i);
        List<Stock_info> list = mapper.selectinfo(stock);
        System.out.println("list==============" + list.toString());
        Integer totalstocks = mapper.selectinfoTotal(stock);
        System.out.println("总数==============" + totalstocks);
        if (list.size() > 0) {
            pagesBean.setData(list);
            pagesBean.setDataCount(totalstocks);
            return ApiResult.successWithData(pagesBean);
        } else {
            return ApiResult.failure("查询失败");
        }
    }


    @Override
    public ApiResult updateinfo(Stock_info stock) {
//        stock.setTime(new Date());
        Integer i = mapper.updateinfo(stock);
        if (i > 0) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("查询失败");
        }
    }


    @Override
    public ApiResult addinfo(Stock stock) {
        Integer j = mapper.addStockInfo(stock);
        Integer i = mapper.addStockNumber(stock);
        if (j > 0) {
            return ApiResult.success("成功");
        } else {
            return ApiResult.failure("添加失败");
        }
    }

    @Override
    public List<Stock> selectByTBId(Stock stock) {

        return mapper.selectByTBId(stock);
    }

    @Override
    public ApiResult updateStockNmuber(String orders_no) {
        Integer i =null;
        if(orders_no !=null && "".equals(orders_no)) {
             i = Integer.parseInt(orders_no);
        }else {
            return  ApiResult.failure("修改库存失败");
        }
        List<OrderGoodsNumebrBeans> number = mapper.selectByOrderNo(i);
        Integer integer = null;
        if(!number.isEmpty()){
        for (OrderGoodsNumebrBeans o : number) {
            Integer totalnumber=mapper.selectNumerById(o.getStock_id());
            if(totalnumber>o.getGoods_number()) {
                integer = mapper.reduceStock(o);
                if(integer==0){
                    return  ApiResult.failure("修改库存失败");
                   }
            }else{
                testSender.send(500,"库存出错。请联系库存管理员");
            }
        }
        }
        return null;
    }
}
